package nccloud.web.hrwa.psndocwadoc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import nc.hr.utils.ResHelper;
import nc.itf.hr.wa.IPsndocwadocManageService;
import nc.itf.hr.wa.IPsndocwadocQueryService;
import nc.vo.hi.wadoc.PsndocWadocVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pub.lang.UFLiteralDate;
import nc.vo.uif2.LoginContext;
import nccloud.base.exception.ExceptionUtils;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hrwa.psndocwadoc.vo.DefdocRefDatas;
import nccloud.web.uapbd.commons.web.ParamUtils;









public class PsndocImportInDBAction
  extends HRCommonAction
{
  public <T> Object execute(IRequest request, T para) throws Exception {
    ParamUtils param = new ParamUtils(request);
    LoginContext loginContext = HRNccUtils.getLoginContext(param);
    
    List list = param.getList("datas", null);
    if (null == list || list.isEmpty()) {
      ExceptionUtils.wrapBusinessException(ResHelper.getString("60130adjmtc", "060130adjmtc0176"));
    }
    
    PsndocWadocVO[] psndocwadocvos = new PsndocWadocVO[list.size()];
    Set<String> set = new HashSet<String>();

    
    DefdocRefDatas defdocRefDatas = new DefdocRefDatas();
    Vector<Vector> datas = null;
    
    for (int i = 0; i < list.size(); i++) {
      JSONObject obj = JSON.parseObject(JSON.toJSONString(list.get(i)));
      
      PsndocWadocVO psndocWadocVO = new PsndocWadocVO();
      
      psndocWadocVO.setPsnCode(obj.getString("psnCode"));
      
      psndocWadocVO.setPsnName(obj.getString("psnName"));
      
      if (StringUtils.isBlank(obj.getString("partflag")) || 
        StringUtils.isBlank(obj.getString("assgid"))) {
        psndocWadocVO.setPartflag(UFBoolean.FALSE);
        psndocWadocVO.setAssgid(Integer.valueOf(1));
      } else {
        psndocWadocVO.setPartflag(UFBoolean.valueOf(obj.getString("partflag")));
        psndocWadocVO.setAssgid(Integer.valueOf(Integer.parseInt(obj.getString("assgid"))));
      } 
      
      psndocWadocVO.setPk_wa_item_showname(obj.getString("pk_wa_item_showname"));
      
      psndocWadocVO.setPk_wa_grd_showname(obj.getString("pk_wa_grd_showname"));
      
      if (StringUtils.isNotBlank(obj.getString("begindate"))) {
        psndocWadocVO.setBegindate(new UFLiteralDate(obj.getString("begindate")));
      }
      
      if (StringUtils.isNotBlank(obj.getString("enddate"))) {
        psndocWadocVO.setEnddate(new UFLiteralDate(obj.getString("enddate")));
      }
      
      if (StringUtils.isNotBlank(obj.getString("changedate"))) {
        psndocWadocVO.setChangedate(new UFLiteralDate(obj.getString("changedate")));
      }
      
      psndocWadocVO.setNegotiation_wage(UFBoolean.valueOf(obj.getString("negotiation_wage")));
      
      psndocWadocVO.setWa_prmlv(obj.getString("wa_prmlv"));
      
      psndocWadocVO.setWa_seclv(obj.getString("wa_seclv"));
      
      psndocWadocVO.setNmoney(new UFDouble(obj.getString("nmoney")));
      
      psndocWadocVO.setWaflag(UFBoolean.valueOf(obj.getString("waflag")));

      
      String changecause = obj.getString("pk_changecause");
      if (StringUtils.isNotBlank(changecause)) {
        String resultValue = "";
        if (null != datas && !datas.isEmpty()) {
          resultValue = defdocRefDatas.matchPkChangecause(datas, changecause);
        }
        if (StringUtils.isBlank(resultValue)) {
          datas = defdocRefDatas.getDefDocRefDatas(loginContext.getPk_org(), "nc.ui.bd.ref.model.DefdocGridRefModel");
          resultValue = defdocRefDatas.matchPkChangecause(datas, changecause);
        } 
        psndocWadocVO.setPk_changecause(resultValue);
      } else {
        psndocWadocVO.setPk_changecause(null);
      } 
      String pk_jobgrade= obj.getString("pk_jobgrade");
      if (StringUtils.isNotEmpty(pk_jobgrade)){
    	  psndocWadocVO.setPk_jobgrade(pk_jobgrade);
      }
      String pk_jobrank = obj.getString("pk_jobrank");
      if (StringUtils.isNotEmpty(pk_jobrank)){
    	  psndocWadocVO.setPk_jobrank(pk_jobrank);
      }
      
      psndocWadocVO.setVbasefile(obj.getString("vbasefile"));
      
      psndocWadocVO.setVnote(obj.getString("vnote"));

      
      if (psndocWadocVO.getNegotiation_wage().booleanValue()) {
        psndocWadocVO.setPk_wa_grd(null);
        psndocWadocVO.setPk_wa_grd_showname(null);
        psndocWadocVO.setPk_wa_crt(null);
        psndocWadocVO.setPk_wa_crt_showname(null);
        psndocWadocVO.setPk_wa_prmlv(null);
        psndocWadocVO.setPk_wa_seclv(null);
      } 
      
      psndocWadocVO.setIadjustmatter(Integer.valueOf(1));
      psndocWadocVO.setPk_group(loginContext.getPk_group());
      psndocWadocVO.setPk_org(loginContext.getPk_org());
      psndocWadocVO.setLastflag(UFBoolean.valueOf("Y"));
      psndocWadocVO.setWorkflowflag(UFBoolean.valueOf("N"));
      set.add(psndocWadocVO.getPsnCode());
      psndocwadocvos[i] = psndocWadocVO;
    } 
    
    int importBefLen = psndocwadocvos.length;
    Arrays.sort(psndocwadocvos);
    
    IPsndocwadocManageService service = (IPsndocwadocManageService)ServiceLocator.find(IPsndocwadocManageService.class);
    PsndocWadocVO[] failtureReports = service.importData2DB(psndocwadocvos, loginContext);
    
    int importAftLen = failtureReports.length;
    StringBuffer sbMessage = new StringBuffer();
    sbMessage.append(ResHelper.getString("60130adjmtc", "060130adjmtc0177", new String[] {
            String.valueOf(importBefLen - importAftLen), 
            String.valueOf(importAftLen)
          }));
    sbMessage.append("\n");
    Arrays.sort(failtureReports);
    
    IPsndocwadocQueryService queryService = (IPsndocwadocQueryService)ServiceLocator.find(IPsndocwadocQueryService.class);
    PsndocWadocVO[] reVos = queryService.queryPsndocWadocsByPsncode((String[])set.toArray(new String[set.size()]));
    
    queryService.psndocWadocSaveToWainfoVO(reVos);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("message", sbMessage.toString());
    if (importAftLen > 0) {


      
      sbMessage.append(ResHelper.getString("60130adjmtc", "060130adjmtc0180"));
      
      map.put("errorNum", Integer.valueOf(importAftLen));
      map.put("errorDatas", failtureReports);
    } 
    return map;
  }
}